class Solution1 {
    public static void main(String[] args) {
        int[] arr = {3,3,3,1,2,1,1,2,3,3,4};
        System.out.println(totalFruit(arr));
    }
    public static int totalFruit(int[] fruits) {
       int[] set = new int[fruits.length];
       int count = 0 ; // 记录类型 
       int sum = 0 ; // 记录最后结果

       for(int i = 0 , j = 0 ; j < fruits.length ; j ++){
            if(set[fruits[j]]++ == 0){
                count ++ ;
            }
            while(count > 2){
                set[fruits[i]] --;
                if(set[fruits[i++]] == 0){
                    count -- ;
                }
            }
           sum = Math.max(sum , j-i+1);
       }  
      return sum ;
    }
}